#ifndef __Map_h__
#define __Map_h__

#include <windows.h>
#include <map>

#include "Tile.h"

//              (x1, y1)
//                  |
//         (1)  /   |   \  (3)
//             /    |    \
//  (x2, y2) ---------------- (x3, y3)
//             \    |    /
//         (4)  \   |   /  (2)
//                  |
//              (x4, y4)

struct TileCoordinate
{
    TileCoordinate():
    m_XTop(NULL),
    m_YTop(NULL),
    m_XBottom(NULL),
    m_YBottom(NULL),
    m_XLeft(NULL),
    m_YLeft(NULL),
    m_XRight(NULL),
    m_YRight(NULL)
    {
    }

    UINT m_XTop;
    UINT m_YTop;
    UINT m_XBottom;
    UINT m_YBottom;
    UINT m_XLeft;
    UINT m_YLeft;
    UINT m_XRight;
    UINT m_YRight;
};

class CMap
{
public:
    CMap();

    // key : tile index, value : tile
    typedef std::map<UINT, CTile>                             vContainer;
    typedef std::map<UINT, CTile>::iterator                   vContainerItor;
    typedef std::pair<UINT, CTile>                            vContainerInsert;

    vContainer m_Container;

    CTile* CMap::AddTile(UINT Line, UINT Column);
    
    UINT GetTileIndex(FLOAT X, FLOAT Y);

    void GetTileCoordinate(const CTile& Tile, TileCoordinate& ATileCoordinate);

    void SetTileTextureByIndex(UINT Index, std::string Texture);

    void SetTileWallByIndex(UINT Index);

    BOOL LineEquation1(FLOAT X, FLOAT Y, FLOAT X1, FLOAT Y1, FLOAT X2, FLOAT Y2);

    BOOL LineEquation2(FLOAT X, FLOAT Y, FLOAT X3, FLOAT Y3, FLOAT X4, FLOAT Y4);

    BOOL LineEquation3(FLOAT X, FLOAT Y, FLOAT X1, FLOAT Y1, FLOAT X3, FLOAT Y3);

    BOOL LineEquation4(FLOAT X, FLOAT Y, FLOAT X2, FLOAT Y2, FLOAT X4, FLOAT Y4);

    BOOL Writer(LPCWSTR);

    BOOL Reader(LPCWSTR);

    INT m_TileWidth;        // tiles size

    INT m_TileHeight;       // tiles size
};

CMap& _GetMap();
void _SaveMap(LPCWSTR FilePath);
void _LoadMap(LPCWSTR FilePath);

#endif